/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mobsandgeeks.adapters.samples.fraction;

import com.mobsandgeeks.adapters.InstantAdapter;
import com.mobsandgeeks.adapters.Sectionizer;
import com.mobsandgeeks.adapters.SimpleSectionAdapter;
import com.mobsandgeeks.adapters.samples.Book;
import com.mobsandgeeks.adapters.samples.BooksDao;
import com.mobsandgeeks.adapters.samples.ResourceTable;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * ListView based on {@link SimpleSectionAdapter}
 *
 * @since 2021-06-30
 */
public class SectionedListFraction extends Fraction {
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_ability_fraction, container, false);
        inintView(component);
        return component;
    }

    /**
     * inintView
     *
     * @param component
     */
    private void inintView(Component component) {
        ArrayList<Book> books = new ArrayList<Book>(BooksDao.getAllBooks());
        Collections.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book lhs, Book rhs) {
                return lhs.getBookCategory().compareTo(rhs.getBookCategory());
            }
        });
        InstantAdapter<Book> bookListAdapter = new InstantAdapter<Book>(
                getFractionAbility(), ResourceTable.Layout_list_layout, Book.class, books);

        SimpleSectionAdapter<Book> sectionedAdapter =
                new SimpleSectionAdapter<Book>(getFractionAbility(),
                        bookListAdapter,
                        ResourceTable.Layout_section_header,
                        ResourceTable.Id_section_text,
                        new Sectionizer<Book>() {
                    @Override
                    public String getSectionTitleForItem(Book instance) {
                        return instance.getBookCategory();
                    }
                });
        ListContainer listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_list);
        listContainer.setItemProvider(sectionedAdapter);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
}
